1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.primitives;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.annotations.GwtIncompatible;
21  import com.google.common.collect.testing.Helpers;
22  import com.google.common.testing.NullPointerTester;
23  
24  import junit.framework.TestCase;
25  
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.List;
30  
31  /**
32   * Unit test for {@link Bytes}.
33   *
34   * @author Kevin Bourrillion
35   */
36  @GwtCompatible(emulated = true)
37  public class BytesTest extends TestCase {
38    private static final byte[] EMPTY = {};
39    private static final byte[] ARRAY1 = {(byte) 1};
40    private static final byte[] ARRAY234
41        = {(byte) 2, (byte) 3, (byte) 4};
42  
43    private static final byte[] VALUES =
44        { Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE };
45  
46    public void testHashCode() {
47      for (byte value : VALUES) {
48        assertEquals(((Byte) value).hashCode(), Bytes.hashCode(value));
49      }
50    }
51  
52    public void testContains() {
53      assertFalse(Bytes.contains(EMPTY, (byte) 1));
54      assertFalse(Bytes.contains(ARRAY1, (byte) 2));
55      assertFalse(Bytes.contains(ARRAY234, (byte) 1));
56      assertTrue(Bytes.contains(new byte[] {(byte) -1}, (byte) -1));
57      assertTrue(Bytes.contains(ARRAY234, (byte) 2));
58      assertTrue(Bytes.contains(ARRAY234, (byte) 3));
59      assertTrue(Bytes.contains(ARRAY234, (byte) 4));
60    }
61  
62    public void testIndexOf() {
63      assertEquals(-1, Bytes.indexOf(EMPTY, (byte) 1));
64      assertEquals(-1, Bytes.indexOf(ARRAY1, (byte) 2));
65      assertEquals(-1, Bytes.indexOf(ARRAY234, (byte) 1));
66      assertEquals(0, Bytes.indexOf(
67          new byte[] {(byte) -1}, (byte) -1));
68      assertEquals(0, Bytes.indexOf(ARRAY234, (byte) 2));
69      assertEquals(1, Bytes.indexOf(ARRAY234, (byte) 3));
70      assertEquals(2, Bytes.indexOf(ARRAY234, (byte) 4));
71      assertEquals(1, Bytes.indexOf(
72          new byte[] { (byte) 2, (byte) 3, (byte) 2, (byte) 3 },
73          (byte) 3));
74    }
75  
76    public void testIndexOf_arrayTarget() {
77      assertEquals(0, Bytes.indexOf(EMPTY, EMPTY));
78      assertEquals(0, Bytes.indexOf(ARRAY234, EMPTY));
79      assertEquals(-1, Bytes.indexOf(EMPTY, ARRAY234));
80      assertEquals(-1, Bytes.indexOf(ARRAY234, ARRAY1));
81      assertEquals(-1, Bytes.indexOf(ARRAY1, ARRAY234));
82      assertEquals(0, Bytes.indexOf(ARRAY1, ARRAY1));
83      assertEquals(0, Bytes.indexOf(ARRAY234, ARRAY234));
84      assertEquals(0, Bytes.indexOf(
85          ARRAY234, new byte[] { (byte) 2, (byte) 3 }));
86      assertEquals(1, Bytes.indexOf(
87          ARRAY234, new byte[] { (byte) 3, (byte) 4 }));
88      assertEquals(1, Bytes.indexOf(ARRAY234, new byte[] { (byte) 3 }));
89      assertEquals(2, Bytes.indexOf(ARRAY234, new byte[] { (byte) 4 }));
90      assertEquals(1, Bytes.indexOf(new byte[] { (byte) 2, (byte) 3,
91          (byte) 3, (byte) 3, (byte) 3 },
92          new byte[] { (byte) 3 }
93      ));
94      assertEquals(2, Bytes.indexOf(
95          new byte[] { (byte) 2, (byte) 3, (byte) 2,
96              (byte) 3, (byte) 4, (byte) 2, (byte) 3},
97          new byte[] { (byte) 2, (byte) 3, (byte) 4}
98      ));
99      assertEquals(1, Bytes.indexOf(
100         new byte[] { (byte) 2, (byte) 2, (byte) 3,
101             (byte) 4, (byte) 2, (byte) 3, (byte) 4},
102         new byte[] { (byte) 2, (byte) 3, (byte) 4}
103     ));
104     assertEquals(-1, Bytes.indexOf(
105         new byte[] { (byte) 4, (byte) 3, (byte) 2},
106         new byte[] { (byte) 2, (byte) 3, (byte) 4}
107     ));
108   }
109 
110   public void testLastIndexOf() {
111     assertEquals(-1, Bytes.lastIndexOf(EMPTY, (byte) 1));
112     assertEquals(-1, Bytes.lastIndexOf(ARRAY1, (byte) 2));
113     assertEquals(-1, Bytes.lastIndexOf(ARRAY234, (byte) 1));
114     assertEquals(0, Bytes.lastIndexOf(
115         new byte[] {(byte) -1}, (byte) -1));
116     assertEquals(0, Bytes.lastIndexOf(ARRAY234, (byte) 2));
117     assertEquals(1, Bytes.lastIndexOf(ARRAY234, (byte) 3));
118     assertEquals(2, Bytes.lastIndexOf(ARRAY234, (byte) 4));
119     assertEquals(3, Bytes.lastIndexOf(
120         new byte[] { (byte) 2, (byte) 3, (byte) 2, (byte) 3 },
121         (byte) 3));
122   }
123 
124   public void testConcat() {
125     assertTrue(Arrays.equals(EMPTY, Bytes.concat()));
126     assertTrue(Arrays.equals(EMPTY, Bytes.concat(EMPTY)));
127     assertTrue(Arrays.equals(EMPTY, Bytes.concat(EMPTY, EMPTY, EMPTY)));
128     assertTrue(Arrays.equals(ARRAY1, Bytes.concat(ARRAY1)));
129     assertNotSame(ARRAY1, Bytes.concat(ARRAY1));
130     assertTrue(Arrays.equals(ARRAY1, Bytes.concat(EMPTY, ARRAY1, EMPTY)));
131     assertTrue(Arrays.equals(
132         new byte[] {(byte) 1, (byte) 1, (byte) 1},
133         Bytes.concat(ARRAY1, ARRAY1, ARRAY1)));
134     assertTrue(Arrays.equals(
135         new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 4},
136         Bytes.concat(ARRAY1, ARRAY234)));
137   }
138 
139   public void testEnsureCapacity() {
140     assertSame(EMPTY, Bytes.ensureCapacity(EMPTY, 0, 1));
141     assertSame(ARRAY1, Bytes.ensureCapacity(ARRAY1, 0, 1));
142     assertSame(ARRAY1, Bytes.ensureCapacity(ARRAY1, 1, 1));
143     assertTrue(Arrays.equals(
144         new byte[] {(byte) 1, (byte) 0, (byte) 0},
145         Bytes.ensureCapacity(ARRAY1, 2, 1)));
146   }
147 
148   public void testEnsureCapacity_fail() {
149     try {
150       Bytes.ensureCapacity(ARRAY1, -1, 1);
151       fail();
152     } catch (IllegalArgumentException expected) {
153     }
154     try {
155       // notice that this should even fail when no growth was needed
156       Bytes.ensureCapacity(ARRAY1, 1, -1);
157       fail();
158     } catch (IllegalArgumentException expected) {
159     }
160   }
161 
162   public void testToArray() {
163     // need explicit type parameter to avoid javac warning!?
164     List<Byte> none = Arrays.<Byte>asList();
165     assertTrue(Arrays.equals(EMPTY, Bytes.toArray(none)));
166 
167     List<Byte> one = Arrays.asList((byte) 1);
168     assertTrue(Arrays.equals(ARRAY1, Bytes.toArray(one)));
169 
170     byte[] array = {(byte) 0, (byte) 1, (byte) 0x55};
171 
172     List<Byte> three = Arrays.asList((byte) 0, (byte) 1, (byte) 0x55);
173     assertTrue(Arrays.equals(array, Bytes.toArray(three)));
174 
175     assertTrue(Arrays.equals(array, Bytes.toArray(Bytes.asList(array))));
176   }
177 
178   public void testToArray_threadSafe() {
179     for (int delta : new int[] { +1, 0, -1 }) {
180       for (int i = 0; i < VALUES.length; i++) {
181         List<Byte> list = Bytes.asList(VALUES).subList(0, i);
182         Collection<Byte> misleadingSize =
183             Helpers.misleadingSizeCollection(delta);
184         misleadingSize.addAll(list);
185         byte[] arr = Bytes.toArray(misleadingSize);
186         assertEquals(i, arr.length);
187         for (int j = 0; j < i; j++) {
188           assertEquals(VALUES[j], arr[j]);
189         }
190       }
191     }
192   }
193 
194   public void testToArray_withNull() {
195     List<Byte> list = Arrays.asList((byte) 0, (byte) 1, null);
196     try {
197       Bytes.toArray(list);
198       fail();
199     } catch (NullPointerException expected) {
200     }
201   }
202 
203   public void testToArray_withConversion() {
204     byte[] array = {(byte) 0, (byte) 1, (byte) 2};
205 
206     List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
207     List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
208     List<Integer> ints = Arrays.asList(0, 1, 2);
209     List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
210     List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
211     List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
212 
213     assertTrue(Arrays.equals(array, Bytes.toArray(bytes)));
214     assertTrue(Arrays.equals(array, Bytes.toArray(shorts)));
215     assertTrue(Arrays.equals(array, Bytes.toArray(ints)));
216     assertTrue(Arrays.equals(array, Bytes.toArray(floats)));
217     assertTrue(Arrays.equals(array, Bytes.toArray(longs)));
218     assertTrue(Arrays.equals(array, Bytes.toArray(doubles)));
219   }
220 
221   public void testAsList_isAView() {
222     byte[] array = {(byte) 0, (byte) 1};
223     List<Byte> list = Bytes.asList(array);
224     list.set(0, (byte) 2);
225     assertTrue(Arrays.equals(new byte[] {(byte) 2, (byte) 1}, array));
226     array[1] = (byte) 3;
227     assertEquals(Arrays.asList((byte) 2, (byte) 3), list);
228   }
229 
230   public void testAsList_toArray_roundTrip() {
231     byte[] array = { (byte) 0, (byte) 1, (byte) 2 };
232     List<Byte> list = Bytes.asList(array);
233     byte[] newArray = Bytes.toArray(list);
234 
235     // Make sure it returned a copy
236     list.set(0, (byte) 4);
237     assertTrue(Arrays.equals(
238         new byte[] { (byte) 0, (byte) 1, (byte) 2 }, newArray));
239     newArray[1] = (byte) 5;
240     assertEquals((byte) 1, (byte) list.get(1));
241   }
242 
243   // This test stems from a real bug found by andrewk
244   public void testAsList_subList_toArray_roundTrip() {
245     byte[] array = { (byte) 0, (byte) 1, (byte) 2, (byte) 3 };
246     List<Byte> list = Bytes.asList(array);
247     assertTrue(Arrays.equals(new byte[] { (byte) 1, (byte) 2 },
248         Bytes.toArray(list.subList(1, 3))));
249     assertTrue(Arrays.equals(new byte[] {},
250         Bytes.toArray(list.subList(2, 2))));
251   }
252 
253   public void testAsListEmpty() {
254     assertSame(Collections.emptyList(), Bytes.asList(EMPTY));
255   }
256 
257   @GwtIncompatible("NullPointerTester")
258   public void testNulls() {
259     new NullPointerTester().testAllPublicStaticMethods(Bytes.class);
260   }
261 }